package io.github.jast90.internship.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import io.github.jast90.internship.entity.InternshipWeekly;
import io.github.jast90.internship.entity.User;
import io.github.jast90.internship.mapper.InternshipWeeklyMapper;
import io.github.jast90.internship.mapper.UserMapper;
import io.github.jast90.web.common.result.CommonResult;
import io.github.jast90.web.common.result.MyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jast90 on 2021/3/12
 */
@Service
public class ExcelService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private InternshipWeeklyMapper internshipWeeklyMapper;

    private <T> void  exportFile(HttpServletResponse response, String fileName, Class clazz,String sheetName, List<T> data){
        response.setHeader("content-disposition", "attachment;filename="+fileName);
        response.setContentType("application/vnd.ms-excel");
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            EasyExcel.write(outputStream, clazz).sheet(sheetName).doWrite(data);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(outputStream!=null){
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 导出用户模版excel
     * @param response
     */
    public void exportUserTemplate(HttpServletResponse response){
        String fileName =  "user-template-" + System.currentTimeMillis() + ".xlsx";
        exportFile(response,fileName,User.class,"用户",new ArrayList<>());
    }

    public CommonResult uploadUsers(MultipartFile multipartFile){
        CommonResult result = new CommonResult(MyResult.FAIL);
        ExcelReader excelReader = null;
        try {
            excelReader = EasyExcel.read(multipartFile.getInputStream(), User.class,
                    new UserListener(userMapper,passwordEncoder)).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
            result = new CommonResult(MyResult.SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (excelReader != null) {
                // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                excelReader.finish();
            }
        }
        return result;
    }

    public void exportAllInternshipWeekly(HttpServletResponse response){
        String fileName =  "Internship weekly-" + System.currentTimeMillis() + ".xlsx";
        List<InternshipWeekly> internshipWeeklies = internshipWeeklyMapper.selectAll();
        exportFile(response,fileName, InternshipWeekly.class,"实习周记",internshipWeeklies);
    }
}
